package php4dvdPages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import php4dvdTests.MyPageFactory;

import java.util.List;

import static java.text.MessageFormat.format;


/**
 * @author Anna Alexeyenko
 *         Date: 1/26/13
 */
public class HomePage extends InternalPage {

    @FindBy(id = "search")
    private WebElement searchInput;

    @FindBy(xpath = "//section//a[contains(@href, \"/?go=add\")]")
    private WebElement addMovieButton;

    @FindBy(xpath = "//section//a[contains(@href, \"/?go=imdbupdate\")]")
    private WebElement updateAllButton;


    @FindBy(xpath = "//section//a[contains(@href, \"/?go=export\")]")
    private WebElement exportButton;

    @FindBy(xpath = "//*[@id='results']/a")
    private List<WebElement> linksToFilms;


    public boolean isOnThisPage() {
        return searchInput.isDisplayed();
    }

    public MoviePage openMoviePage(String title) throws Exception {
        for (WebElement movie : linksToFilms) {
            if (title.equals(movie.findElement(By.className("title")).getText())) {
                movie.click();
                return MyPageFactory.getPage(driver, MoviePage.class);
            }
        }
        throw new Exception(format("There is no movie with title \"{0}\" in the list", title));

    }

    public AddMoviePage goToAddMoviePage() {
        addMovieButton.click();
        return MyPageFactory.getPage(driver, AddMoviePage.class);

    }
}
